/**
 * @Author: wilmerxu
 * @Description: 消息通信
 * @Date: 2020/7/28 15:16
 */
class ExtMessage {
  // 向content.js发送消息
  sendContentMessage(cmd, value) {
    return new Promise((resolve => {
      chrome.tabs.query({active: true, currentWindow: true}, (tabs) => {
        chrome.tabs.sendMessage(tabs[0].id, {cmd, value}, (response) => {
          resolve(response);
        });
      });
    }));
  }

  // 发送内容消息
  contentMessage(msg) {
    return new Promise((resolve => {
      chrome.tabs.query({active: true, currentWindow: true}, (tabs) => {
        chrome.tabs.sendMessage(tabs[0].id, {cmd: 'content-message', value: msg}, (response) => {
          resolve(response);
        });
      });
    }));
  }

  // 向background.js发送消息
  sendBackgroundMessage(cmd, value) {
    return new Promise((resolve => {
      chrome.tabs.query({active: true, currentWindow: true}, (tabs) => {
        chrome.tabs.sendMessage(tabs[0].id, {cmd, value}, (response) => {
          resolve(response);
        });
      });
    }));
  }
}

export default ExtMessage;
